package function;

import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.FunctionHint;
import org.apache.flink.table.functions.TableFunction;
import org.apache.flink.types.Row;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

@FunctionHint(output = @DataTypeHint("ROW<word STRING>"))
public class splitWordsFunction extends TableFunction<Row> {

    public void eval(String str) throws IOException {
        //创建集合用于存放结果数据
        ArrayList<String> resultList = new ArrayList<>();

        StringReader reader = new StringReader(str);
        IKSegmenter ikSegmenter = new IKSegmenter(reader, false);
        //遍历分词结果并放入结合中
        while (true){
            Lexeme next = ikSegmenter.next();
            if (next != null) {
                String word = next.getLexemeText();
                resultList.add(word);
            } else {
                break;
            }
        }
        //遍历写出
        for (int i=0;i<resultList.size();i++){
            collect(Row.of(resultList.get(i)));
        }

    }
}
